<?php

namespace App\Http\Controllers\Admin\Config;

use Illuminate\Http\Request;
use App\Http\Controllers\AdminController;

use App\Models\Admin\Role;
use App\Models\Admin\Admin;
use DB;
use Auth;

class AdminUserController extends AdminController
{

    /**
     * 获取列表数据
     * @return [type] [description]
     */
    function getLists(Request $request){
    	$pageIndex = (int)$request->input('page') ?  $request->input('page') : 1;
    	$length = 10;
    	$start = ($pageIndex-1)*$length;

    	$where = ['masterIs' => 0];
    	$data['total'] = Admin::where($where)->count();
    	if($data['total']>0){
    		$data['result'] = Admin::where($where)->orderBy('name','desc')->skip($start)->take($length)->get()->toArray();
    	}
    	$this->ajaxJson(0,'',$data);
    }

    /**
     * 获取用户
     * @return [type] [description]
     */
    function getAdmin($id){
        $data = Admin::where('id',$id)->with('roles')->first();
        $this->ajaxJson(0,'',$data);
    }

    /**
     * 获取所有角色
     * @return [type] [description]
     */
    function getRoleAll(){
        $this->ajaxJson(0,'',Role::orderBy('name','desc')->orderBy('updated_at','desc')->get()->toArray());
    }

    /**
     * 更新、添加
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    function postUpdate(Request $request){
    	$id = $request->input('id');
    	$name = $request->input('name');
    	$this->_check_name($id,$name);
    	if($id){
    		$admin = $this->_get_admin($id);
    	}else{
    		$admin = New Admin();
    		$admin->name = $request->input('name');
    	}
    	if($request->input('password')){
    		$admin->password = bcrypt($request->input('password'));
    	}
    	$admin->masterIs = $request->input('masterIs')=='true' ? 1 : 0;
    	$admin->save();
    	$admin->roles()->sync($request->input('roles'));
    	if($admin->id==Auth::guard('admin')->user()->id){
    		Auth::guard('admin')->logout();
        	$request->session()->flush();
        	$request->session()->regenerate();
    	}
    	return $this->ajaxJson(0,'更新成功',[],false);
    }

    /**
     * 删除用户
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    function deleteOne($id){
    	$admin = $this->_get_admin($id);
    	if(Auth::guard('admin')->user()->id==$id){
    		$this->ajaxJson(1,'无法删除本人');
    	}
    	$admin->delete();
    	$admin->roles()->detach();
    	$this->ajaxJson(0,'删除成功');
    }

    /**
     * 验证用户名是否已用
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    function checkName(Request $request){
    	$this->_check_name($request->input('id'),$request->input('name'));
    	$this->ajaxJson();
    }


    /**
     * 获取单个后台用户
     * @return [type] [description]
     */
    private function _get_admin($id){
    	$admin = Admin::find($id);
    	if(!$admin){
    		$this->ajaxJson(1,'不存在的用户');
    	}
    	return $admin;
    }

    /**
     * 验证用户名唯一
     * @param  [type] $name [description]
     * @return [type]       [description]
     */
    private function _check_name($id,$name){
    	$admin = Admin::where('id','!=',$id)->where('name',$name)->first();
    	if($admin){
    		$this->ajaxJson(1,'登录名已存在');
    	}
    }
}
